package com.itqianfeng.smartmeeting.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itqianfeng.smartmeeting.entity.Booking;
import com.itqianfeng.smartmeeting.entity.dto.BookingDTO;
import com.itqianfeng.smartmeeting.entity.vo.MyBooking;
import com.itqianfeng.smartmeeting.service.BookingService;
import com.itqianfeng.smartmeeting.service.TimeslotService;
import com.itqianfeng.smartmeeting.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/booking")
public class BookingController {
    @Autowired
    private BookingService bookingService;
    @Autowired
    private TimeslotService timeslotService;
    @GetMapping("/list")
    public Map list(@RequestParam(name = "page") int page, @RequestParam(name = "limit") int limit){
        try{
            Page<Booking> page1 = new Page<>(page, limit);
            IPage<Booking> obj = bookingService.page(page1);
            return CommonResult.ok("查询失败",obj);
        } catch (Exception e){
            e.printStackTrace();
            return CommonResult.error("发生异常",-1);
        }
    }
    @PostMapping("/add")
    public Map add(@RequestBody Booking booking){
        try{
            boolean save = bookingService.save(booking);
            return CommonResult.ok(save?"添加成功":"添加失败",null);
        } catch (Exception e){
            e.printStackTrace();
            return CommonResult.error("发生异常",-1);
        }
    }
    @PutMapping("/modify")
    public Map updateState(int id,int state){
        try{
            UpdateWrapper<Booking> wrapper=new UpdateWrapper<>();
            wrapper.set("state",state);
            wrapper.eq("id",id);
            boolean update = bookingService.update(wrapper);
            return CommonResult.ok(update?"修改成功":"修改失败",null);
        } catch (Exception e){
            e.printStackTrace();
            return CommonResult.error("发生异常",-1);
        }
    }
   //我的预约
    @GetMapping("/myBooking/{userId}")
    public Map myBooking(@PathVariable(name = "userId") Integer userId){
        try{
         List<MyBooking> list= bookingService.getMyBooking(userId);
           return CommonResult.ok("查询成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.error("发生异常",-1);
        }
    }
    @PostMapping("/saveBooking")
    @Transactional
    public Map saveBooking(@RequestBody BookingDTO bookingDTO){
        try{
            Boolean save = bookingService.saveBooking(bookingDTO);
            return  CommonResult.ok(save?"添加成功":"添加失败",null);
        } catch (Exception e){
            e.printStackTrace();
            return  CommonResult.error("发生异常",-1);
        }
    }
    @PutMapping("/updateState")
    public Map updateState(Integer userId,Integer roomId,Integer timeId,int state){
        try{
            UpdateWrapper<Booking> wrapper=new UpdateWrapper<>();
            wrapper.set("state",state);
            wrapper.eq("user_id",userId);
            wrapper.eq("room_id",roomId);
            wrapper.eq("time_id",timeId);
            boolean update = bookingService.update(wrapper);
            return CommonResult.ok(update?"修改成功":"修改失败",null);
        } catch (Exception e){
            return CommonResult.error("发生异常",-1);
        }
    }
    @GetMapping("/isBooking")
    public Map isBooking(Integer roomId,String startTime,String endTime){
        try{
            Integer count = bookingService.isBooking(roomId , startTime , endTime);
            return CommonResult.ok(count==0?"可以预约":"不可以预约",null);
        } catch (Exception e){
            e.printStackTrace();
            return CommonResult.error("发生异常",-1);
        }
    }
}
